﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SerialTools
{
    public partial class FrmAddEditCmd : Form
    {
        public FrmAddEditCmd()
        {
            InitializeComponent();
        }

        public int CmdIndex;

        private void FrmAddEditCmd_Load(object sender, EventArgs e)
        {
            BatchSendCmd cmd = Program.Config.BatchCmd[CmdIndex];

            tbxName.Text = cmd.Name;
            rtbxSend.Text = cmd.SendBuff;
            if (cmd.DataType == 2)
                rbtnHex.Checked = true;
            else
                rbtnAsc.Checked = true;
            cbxAppend.SelectedIndex = cmd.EndType switch
            {
                "" => 0,
                "return" => 1,
                "newline" => 2,
                "retnewline" => 3,
                _ => cbxAppend.SelectedIndex
            };
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Program.Config.BatchCmd[CmdIndex].Name = tbxName.Text.Trim();
            Program.Config.BatchCmd[CmdIndex].SendBuff = rtbxSend.Text.Trim();
            Program.Config.BatchCmd[CmdIndex].DataType = rbtnHex.Checked ? 2 : 1;
            Program.Config.BatchCmd[CmdIndex].EndType = cbxAppend.SelectedIndex switch
            {
                0 => "",
                1 => "return",
                2 => "newline",
                3 => "retnewline",
                _ => Program.Config.BatchCmd[CmdIndex].EndType
            };
            this.Close();
        }
    }
}
